from selenium import webdriver
from time import sleep
from selenium.webdriver.support.select import Select

try:
    driver = webdriver.Chrome()
    driver.get('http://localhost/ranzhi/www/sys/user-login.html')
    driver.maximize_window()

    # 登陆
    driver.find_element_by_id('account').send_keys('admin')
    driver.find_element_by_id('password').send_keys('123456')
    driver.find_element_by_id('submit').click()

    sleep(1)
    # 点击后台管理
    driver.find_element_by_id('s-menu-superadmin').click()
    sleep(1)

    # 定位iframe元素
    iframe = driver.find_element_by_id('iframe-superadmin')
    # 切换到iframe中
    driver.switch_to.frame(iframe)
    # 点击 添加成员
    driver.find_element_by_partial_link_text('添加成员').click()
    # 用户名
    driver.find_element_by_id('account').send_keys('tom')

    # 真实姓名
    driver.find_element_by_id('realname').send_keys('Tom Cruse')

    # 性别
    driver.find_element_by_id('genderm').click()

    # 部门
    select = driver.find_element_by_id('dept')
    depts = Select(select)
    depts.select_by_index(2) # 根据下标进行选择，下标从0开始
    depts.select_by_value('10') # 根据value属性的值来进行选择
    depts.select_by_visible_text('/开发部') # 根据可见文本来进行选择

    # 角色
    select = driver.find_element_by_id('role')
    roles = Select(select)
    roles.select_by_value('pm')

    # 密码
    driver.find_element_by_id('password1').send_keys('123456')

    # 密码
    driver.find_element_by_id('password2').send_keys('123456')

    # 邮箱
    driver.find_element_by_id('email').send_keys('tom@163.com')

    # 保存
    driver.find_element_by_id('submit').click()
    sleep(2)

    # 删除
    driver.find_element_by_xpath('/html/body/div/div/div/div[2]/div/div/table/tbody/tr[2]/td[11]/a[3]').click()
    sleep(1)
    # 点击 取消
    driver.switch_to.alert.dismiss()
    # 删除
    driver.find_element_by_xpath('/html/body/div/div/div/div[2]/div/div/table/tbody/tr[2]/td[11]/a[3]').click()
    sleep(1)
    # 点击 确定
    driver.switch_to.alert.accept()

except Exception as e:
    print(e)
finally:
    sleep(2)
    driver.close()